#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <stdio.h>
#include <time.h>
#include "oled.h"
#include "ipaddr.h"
#include "led.h"
#include "humi.h"
#include "key.h"
#include "usart.h"
#include "beep.h"

#include "cirbuff.h"

extern int usart_fd;
CirBuff_Def recvBuff;

enum WARN_FLAG{
	WARN_LOW = 'L',
	WARN_HIG = 'H'
};

void NORM_DHTValue(void);
void WARN_TempValue(char cond);
void WARN_HumiValue(char cond);
void Mode_AllValue(void);
void Mode_TempValue(void);
void Mode_HumiValue(void);

u8 warnFlag = 0;
int is_buzzer = 1;
u8 dht_buffer[5] = {0};   		//DHT11温湿度数据
int temp_MaxV = 60;		  		//温度上限
int temp_MinV = 10;				//温度下限
double temp_value = 0.0;		//实时温度

int humi_MaxV = 80;				//湿度上限
int humi_MinV = 5;				//湿度下限
double humi_value = 0.0;		//温度上限

//使用sprintf()温湿度数据转成字符串的字符数组
char tempStr[8] = {0};
char tempStr_Max[4] = {0};
char tempStr_Min[4] = {0};

char humiStr[8] = {0};
char humiStr_Max[4] = {0};
char humiStr_Min[4] = {0};

int showMode = 0;				//显示模式

PI_THREAD(Serial_readThread)
{
    printf("PI_THREAD(Serial_readThread)\n");
    int v = 0;
    int time = 0;
    while(1)
    {
        if(time++ == 20)
        {
            time = 0;
            v = !v;
            digitalWrite(LED_MSGE,v);
        }

        int size = serialDataAvail(usart_fd);
        if(size > 0)
        {
            for(int i = 0; i < size; i++)
            {
                char res = serialGetchar(usart_fd);
                printf("%c", res);
                CirBuff_writeByte(&recvBuff, res);
            }
            printf("\n");
        }
        delay(20);
    }
}


PI_THREAD(Serial_writeThread)
{
    printf("PI_THREAD(Serial_writeThread)\n");
    while(1)
    {
        CirBuff_getProtol(&recvBuff);
        delay(2);
    }
}

PI_THREAD(key_scanThread)		//按键扫描线程
{
    printf("PI_THREAD(key_scanThread)\n");
    while(1)
    {
        int keyValue = key_scan(0);
        if(keyValue > 0)
        {
            printf("keyV: %d\n", keyValue);
            beep_tips();
            if(keyValue == KV_ERA)
			{
				is_buzzer = !is_buzzer;
                digitalWrite(LED_BEEP,!is_buzzer);
            }else if(keyValue == KV_MOD)
			{
				showMode++;
				showMode%=3;
				if(showMode == 0)
				{
					Mode_AllValue();
				}else if(showMode == 1)
				{
					Mode_TempValue();
				}else if(showMode == 2)
				{
					Mode_HumiValue();
				}
            }else if(keyValue == KV_SUB)
			{
				if(showMode == 1)
				{
					if(temp_MaxV > (temp_MinV+10))
					{
						temp_MaxV-=10;
						sprintf(tempStr_Max, "%d",temp_MaxV);
                        OLED_string(36,32,tempStr_Max,16,1);
					}
				}else if(showMode == 2)
				{
					if(humi_MaxV > (humi_MinV+10))
					{
						humi_MaxV-=10;
						sprintf(humiStr_Max, "%d",humi_MaxV);
                        OLED_string(36,32,humiStr_Max,16,1);
					}
				}
                OLED_display();
            }else if(keyValue == KV_ADD)
			{
				if(showMode == 1)
				{
					if(temp_MaxV < 90)
					{
						temp_MaxV+=10;
                        sprintf(tempStr_Max, "%d",temp_MaxV);
                        OLED_string(36,32,tempStr_Max,16,1);
					}
				}else if(showMode == 2)
				{
					if(humi_MaxV < 90)
					{
						humi_MaxV+=10;
                        sprintf(humiStr_Max, "%d",humi_MaxV);
                        OLED_string(36,32,humiStr_Max,16,1);
					}
				}
                OLED_display();
			}
		}
        delay(200);
    }
}

int main(int argc,char **argv)
{
    if(wiringPiSetup() == -1){
        printf("setup wiringPi failed !");
        return 1;
    }
    printf("Hello world!\n");
    USART_openPort();
    led_init();
    key_init();
    beep_init();
    humi_init();
    OLED_init();
    Mode_AllValue();

    sprintf(tempStr_Max, "%d", temp_MaxV);
    sprintf(tempStr_Min, "%d", temp_MinV);
    sprintf(humiStr_Max, "%d", humi_MaxV);
    sprintf(humiStr_Min, "%d", humi_MinV);

    CirBuff_Init(&recvBuff);

    int res = piThreadCreate(key_scanThread);
    if(res != 0)
    {
        printf("KeyScan didn't start\n");
    }

    res = piThreadCreate(Serial_readThread);
    if(res != 0)
    {
        printf("Serial_readThread didn't start\n");
    }

    res = piThreadCreate(Serial_writeThread);
    if(res != 0)
    {
        printf("Serial_writeThread didn't start\n");
    }

    int v = 0;
	while(1)
	{
        v = !v;
        humi_getValue();
        if(temp_value > temp_MaxV)
        {
            digitalWrite(LED_WARN,v);
            if(is_buzzer) beep_tips();
            WARN_TempValue(WARN_HIG);
        }else if(temp_value < temp_MinV)
        {
            digitalWrite(LED_WARN,v);
            if(is_buzzer) beep_tips();
            WARN_TempValue(WARN_LOW);
        }else if(humi_value > humi_MaxV)
        {
            digitalWrite(LED_WARN,v);
            if(is_buzzer) beep_tips();
            WARN_HumiValue(WARN_HIG);
        }else if(humi_value < humi_MinV)
        {
            digitalWrite(LED_WARN,v);
            if(is_buzzer) beep_tips();
            WARN_HumiValue(WARN_LOW);
        }else
        {
            NORM_DHTValue();
            digitalWrite(LED_WARN, 0);
        }

        if(showMode == 0)
        {
            OLED_string(76,16,tempStr,16,1);
            OLED_string(76,32,humiStr,16,1);
            Mode_AllValue();
        }else if(showMode == 1)
        {
            OLED_string(76,16,tempStr,16,1);
            Mode_TempValue();
        }else if(showMode == 2)
        {
            OLED_string(76,16,humiStr,16,1);
            Mode_HumiValue();
        }
        digitalWrite(LED_DATA,v);
        delay(1000);
    }
	return 0;
}

void NORM_DHTValue(void)
{
    OLED_string(48,48,"Data Dose",16,1);
}

void WARN_TempValue(char cond)
{
	if(cond == WARN_LOW)
	{
        OLED_string(48,48,"Temp Over ",16,1);
    }else if(cond == WARN_HIG)
	{
        OLED_string(48,48,"Temp Lower",16,1);
    }
}
void WARN_HumiValue(char cond)
{
	if(cond == WARN_LOW)
	{
        OLED_string(48,48,"Humi Over ",16,1);
    }else if(cond == WARN_HIG)
	{
        OLED_string(48,48,"Humi Lower",16,1);
    }
}

void Mode_AllValue(void)
{
	OLED_ShowCHinese(16,0,0,16,1);
	OLED_ShowCHinese(32,0,1,16,1);
	OLED_ShowCHinese(48,0,2,16,1);
	OLED_ShowCHinese(64,0,3,16,1);
	OLED_ShowCHinese(80,0,4,16,1);
	OLED_ShowCHinese(96,0,5,16,1);
    OLED_ShowCHinese(112,16,7,16,1);
    OLED_ShowCHinese(112,32,6,16,1);
    OLED_string(0,16,"RT-TempV:",16,1);
    OLED_string(0,32,"RT-HumiV:",16,1);
    OLED_string(0,48,"Tips:",16,1);
    OLED_display();
}

void Mode_TempValue(void)
{
    OLED_ShowCHinese(16,0,0,16,1);
    OLED_ShowCHinese(32,0,1,16,1);
    OLED_ShowCHinese(48,0,2,16,1);
    OLED_ShowCHinese(64,0,3,16,1);
    OLED_ShowCHinese(80,0,4,16,1);
    OLED_ShowCHinese(96,0,5,16,1);
    OLED_ShowCHinese(112,16,7,16,1);
    OLED_string(0,16,"RT-TempV:",16,1);
    OLED_string(0,32,"MAX:",16,1);
    OLED_string(36,32,tempStr_Max,16,1);
    OLED_string(64,32,"MIN:",16,1);
    OLED_string(100,32,tempStr_Min,16,1);
    OLED_string(0,48,"Tips:",16,1);
    OLED_display();
}


void Mode_HumiValue(void)
{
    OLED_ShowCHinese(16,0,0,16,1);
    OLED_ShowCHinese(32,0,1,16,1);
    OLED_ShowCHinese(48,0,2,16,1);
    OLED_ShowCHinese(64,0,3,16,1);
    OLED_ShowCHinese(80,0,4,16,1);
    OLED_ShowCHinese(96,0,5,16,1);
    OLED_ShowCHinese(112,16,6,16,1);
    OLED_string(0,16,"RT-HumiV:",16,1);
    OLED_string(0,32,"MAX:",16,1);
    OLED_string(36,32,tempStr_Max,16,1);
    OLED_string(64,32,"MIN:",16,1);
    OLED_string(100,32,tempStr_Min,16,1);
    OLED_string(0,48,"Tips:",16,1);
    OLED_display();
}

